package com.meryl.dtk.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author : Meryl
 * @Description:
 * @Date: Created in 2024/9/6 17:53
 */
public class StringUtil {
    private static Pattern humpPattern = Pattern.compile("[A-Z]");
    private static Pattern linePattern = Pattern.compile("_(\\w)");

    public static String toPascal(String str) {
        str = Character.toLowerCase(str.charAt(0)) + str.substring(1);

        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String toCamel(String str) {
        if (str.indexOf("_") == 0) {
            str = str.substring(1);
        }

        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
